import { ElMessage, ElMessageBox } from 'element-plus';

const createMessage = (type, message, duration, callback) => {
  ElMessage({
    type: type,
    duration: duration,
    message: message,
    onClose: () => {
      if(callback) callback();
    }
  });
};

const confirmMessageBox = (content="", title="", type="info", success=() => {}, fail=() => {}) => {
  ElMessageBox.confirm(
    content,
    title,
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type, // 'success' | 'info' | 'warning' | 'error'
    }
  ).then(() => {
    success();
  }).catch(() => {
    fail();
  });
};

export default {
  error: (message, duration, callback) => {
    createMessage('error', message, duration, callback);
  },
  warning: (message, duration, callback) => {
    createMessage('warning', message, duration, callback);
  },
  success: (message, duration, callback) => {
    createMessage('success', message, duration, callback);
  },
  confirm: (content, title, type, success, fail) => {
    confirmMessageBox(content, title, type, success, fail);
  },
}